* Stata/MP 16.0
* Date: 14th.Oct.2023

set more off

* Adjust the directory paths if necessary!  
global QFII                     "D:\QFII_SSSR_Quarter"
global Data 				    "D:\QFII_SSSR_Quarter\Data\"
global RR3 				        "D:\QFII_SSSR_Quarter\RRFM\"

clear
use ${Data}SSSR_Data_FM.dta,replace

*===============================================================================
************************* Table 1 **************************************
*===============================================================================
cd ${RR3}

sum2docx Comp CompIO CompInc Duration_days Duration_log DISR Participation Turnover AR FIO Size ROA OM BM SG Lev Vsales IRisk Indep DIO using ${RR3}chart1.docx , replace stats(N mean(%9.3f) sd min(%9.3f) median(%9.3f) max(%9.3f) )  title("Descriptive Statistics")

*===============================================================================
****************** Regression results: Table 2 and Column (1) of Table 5 *******************
*===============================================================================
cd ${RR3}

xtset stkcd year 
local y Duration_log Comp CompIO CompInc AR 

forval i=1/5{
	local A:word `i' of `y'
			qui reg `A' FIO Size ROA OM BM SG Lev Vsales IRisk Indep DIO i.year i.Industry,r
			est store full_sample0`i'
}			

outreg2 [ full_sample01 full_sample02 full_sample03 full_sample04] using table2.xls,replace keep( FIO Size ROA OM BM SG Lev Vsales IRisk Indep DIO) addtext(Year,Yes,Industry,Yes) bdec(3) tstat dec(3) e(F)

outreg2 [ full_sample05] using tableAR.xls,replace keep( FIO Size ROA OM BM SG Lev Vsales IRisk Indep DIO) addtext(Year,Yes,Industry,Yes) bdec(3) tstat dec(3) e(F)

*===============================================================================
**** Heckman Two-step Regression results: “Columns (3) and (4) of Table 3”, “Columns (5) of Panel B of Table 4”, and “Column (5) of Table 5” *******************
*===============================================================================
*** Results of the first-step regression
probit QFII_D BPA Size ROA OM BM SG Lev Vsales IRisk Indep DIO i.year i.Industry,r 
est store model0
predict w, xb

outreg2 [model0] using ${RR3}tableHeckman.xls, replace /*
*/ keep( BPA Size ROA OM BM SG Lev Vsales IRisk Indep DIO) /*
*/ sortvar( BPA Size ROA OM BM SG Lev Vsales IRisk Indep DIO) /*
*/ dec(3) tstat addstat(Pseudo R-squared, `e(r2_p)')  /*
*/ addtext(Year FE, Yes, Industry FE, Yes)
est clear

g IMR=normalden(w)/normal(w) if QFII_D==1     
replace IMR =-normalden(w)/normal(-w) if QFII_D==0

*** Results of the second-step regressions
xtset stkcd year 
local y Duration_log Comp DISR AR
forval i = 1/4{
	local A : word `i' of `y'
	reg `A' IMR FIO Size ROA OM BM SG Lev Vsales IRisk Indep DIO i.year i.Industry,r
	est store modela_`i'	
}
outreg2 [modela* ] using ${RR3}tableHeckman.xls, append  /*
*/ keep( QFII_D FIO IMR Size ROA OM BM SG Lev Vsales IRisk Indep DIO  ) /*
*/ sortvar( QFII_D FIO IMR Size ROA OM BM SG Lev Vsales IRisk Indep DIO  ) /*
*/ dec(3) tstat r2  /*
*/ addtext(Year FE, Yes, Industry FE, Yes)
*est clear

outreg2 [modela_4 ] using ${RR3}tableAR.xls, append  /*
*/ keep( QFII_D FIO IMR Size ROA OM BM SG Lev Vsales IRisk Indep DIO  ) /*
*/ sortvar( QFII_D FIO IMR Size ROA OM BM SG Lev Vsales IRisk Indep DIO  ) /*
*/ dec(3) tstat r2  /*
*/ addtext(Year FE, Yes, Industry FE, Yes)
est clear

*===============================================================================
**** 2SLS Regression results: “Columns (5) and (6) of Table 3”, “Columns (6) of Panel B of Table 4”, and “Column (5) of Table 5” *******************
*===============================================================================
xtset stkcd year 
local y Duration_log Comp DISR 
forval i=1/3{
	local A:word `i' of `y'
	ivreg2 `A' (FIO =BPA) Size ROA OM BM SG Lev Vsales IRisk Indep DIO i.year i.Industry ,r ffirst
	outreg2 using table_IV, excel bdec(3) tstat dec(3) e(F) ctitle(second: y=`A') append
}

ivreg2 AR (FIO =BPA) Size ROA OM BM SG Lev Vsales IRisk Indep DIO i.year i.Industry ,r ffirst
outreg2 using ${RR3}tableAR.xls, excel bdec(3) tstat dec(3) e(F) ctitle(second: y=`A') append

*===============================================================================
**** Regression results: Columns (1), (2) and (3) of Panel B of Table 4*******************
*===============================================================================
xtset stkcd year 
local cv Size ROA OM BM SG Lev Vsales IRisk Indep DIO  
local y  DISR Participation Turnover
forval i=1/3{
	local A:word `i' of `y'
			
			qui reg  `A' FIO `cv' i.year i.Industry ,r
			est store full_sample0`i'
}
outreg2 [ single_variable1 full_sample01 single_variable2 full_sample02 single_variable3 full_sample03] ///
using table4.xls,replace keep(FIO `cv' ) addtext(Year,Yes,Industry,Yes) bdec(3) tstat dec(3) e(F)

*===============================================================================
**** Regression results: Table 6*******************
*===============================================================================
****Generate a table displaying the regression results.
****
xtset stkcd year 
local cv Size ROA OM BM SG Lev Vsales IRisk Indep DIO  
local y Duration_log Comp DISR AR
local x FIO_EMI_L FIO_EMI_H FIO_DQ_H FIO_DQ_L FIO_Exchange_H FIO_Exchange_L

forval j=1/6{
local B:word `j' of `x'
	
	forval i=1/4{
	local A:word `i' of `y'

		reg `A' `B' Size ROA OM BM SG Lev Vsales IRisk Indep DIO i.year i.Industry 
		estimates store r`i'`j'
		outreg2 using "table6_suest.xls", append bdec(3) tstat dec(3) e(r2) 
}
}

****Compare the magnitudes of coefficients for FIO_EMI_L and FIO_EMI_H.
xtset stkcd year 
local y Duration_log Comp DISR AR

forval i=1/4{
	local A:word `i' of `y'

	reg `A' FIO_EMI_L Size ROA OM BM SG Lev Vsales IRisk Indep DIO i.year i.Industry
	estimates store r1
	reg `A' FIO_EMI_H Size ROA OM BM SG Lev Vsales IRisk Indep DIO i.year i.Industry 
	estimates store r2
	suest r1 r2,vce(robust)
	test [r1_mean] FIO_EMI_L = [r2_mean] FIO_EMI_H
	return list
}

****Compare the magnitudes of coefficients for FIO_DQ_H and FIO_DQ_L.
xtset stkcd year 
local cv Size ROA OM BM SG Lev Vsales IRisk Indep DIO  
local y Duration_log Comp DISR AR

forval i=1/4{
	local A:word `i' of `y'

	reg `A' FIO_DQ_H Size ROA OM BM SG Lev Vsales IRisk Indep DIO i.year i.Industry
	estimates store r1
	reg `A' FIO_DQ_L Size ROA OM BM SG Lev Vsales IRisk Indep DIO i.year i.Industry 
	estimates store r2
	suest r1 r2,vce(robust)
	test [r1_mean] FIO_DQ_H = [r2_mean] FIO_DQ_L
	return list
}

****Compare the magnitudes of coefficients for FIO_Exchange_H and FIO_Exchange_L.
xtset stkcd year 
local cv Size ROA OM BM SG Lev Vsales IRisk Indep DIO  
local y Duration_log Comp DISR AR

forval i=1/4{
	local A:word `i' of `y'

	reg `A' FIO_Exchange_H Size ROA OM BM SG Lev Vsales IRisk Indep DIO i.year i.Industry
	estimates store r1
	reg `A' FIO_Exchange_L Size ROA OM BM SG Lev Vsales IRisk Indep DIO i.year i.Industry 
	estimates store r2
	suest r1 r2,vce(robust)
	test [r1_mean] FIO_Exchange_H = [r2_mean] FIO_Exchange_L
	return list
}




